<?php
include_once('config/config.php');

$where = 1;
//接收地址的搜索关键字参数
$keyword = $_GET['keyword'];
if(!empty($keyword)){
    $where = "`name` LIKE '%$keyword%'";
}

// 获取地址的当前页参数 => page
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// 获取当前表里数据总数
$count = dbFind("SELECT COUNT(*) AS num FROM `person` WHERE $where");

// 每页显示条数
$limit = 3;

// 页码
$size = 5;

$showPage = page($page,$count['num'],$limit,$size,'yahoo2');

$start = ($page - 1)*$limit;

if(!empty($keyword)){
    $where = "(p.`name` LIKE '%$keyword%') or (j.`name` LIKE '%$keyword%') or (d.`name` LIKE '%$keyword%')";
}

//查询的SQL语句
$sql = "SELECT p.`id`,p.`name`,p.`avatar`,p.`mobile`,p.`sex`,p.`email`,p.`address`,d.`name` as dname, j.`name` as jname  FROM `person` as p LEFT JOIN `department` as d ON p.`depid` = d.`id` LEFT JOIN `job` as j ON p.`jobid` = j.`id` WHERE $where ORDER BY p.`id` ASC LIMIT $start,$limit";
$allData = dbAll($sql);

if ($_POST) {
    $action = $_POST['action'];
    if ($action == 'del') {
        $id = $_POST['id'];
        $admin = dbFind("SELECT * FROM `person` WHERE `id` = '$id'");
        if (!$admin) {
            error("该员工无法删除");
        }

        //登录的员工是否是被删除，没有登录功能
        $result = dbDel('person', "`id` = '$id'");
        if ($result) {
            @is_file($admin['avatar']) && @unlink($admin['avatar']);
            success('删除成功');
        } else {
            error('删除失败');
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <!-- 引用公共头部 -->
    <?php include_once('common/meta.php'); ?>

    <style>
        .icon-plus {
            margin-right: 5px;
        }

        .icon-pencil {
            position: absolute;
            margin-left: -30px;
            margin-top: 38px;
        }

        .icon-remove {
            margin-top: 38px;
        }

        .well {
            padding-bottom: 0;
        }

        .table {
            margin-bottom: 0;
        }
        .search {
            margin-left: 10px;
        }

        .btn-info:focus {
            outline: none;
        }
    </style>
</head>

<body>
    <meta charset="utf-8">
    <title>员工</title>

    <!-- 引用公共头部 -->
    <?php include_once('common/header.php'); ?>

    <!-- 引用侧边菜单 -->
    <?php include_once('common/sidebar.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">员工列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">首页</a> <span class="divider">/</span></li>
            <li class="active">员工</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar" style="display:flex;height:40px">
                    <button class="btn btn-primary" onClick="location='personAdd.php'" style="height: 30px;"><i class="icon-plus"></i>添加员工</button>
                    <form method="get">
                        <input class="search" type="search" name="keyword" placeholder="请输入搜索内容">
                        <button class="btn btn-info" style="margin-top: -8px; background-position: 0;">搜索</button>
                    </form>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>名字</th>
                                <th>头像</th>
                                <th>手机号码</th>
                                <th>性别</th>
                                <th>邮箱</th>
                                <th>地址</th>
                                <th>部门</th>
                                <th>职位</th>
                                <th style="width: 26px;"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            foreach ($allData as $data) {
                                switch ($data['sex']) {
                                    case 0:
                                        $data['sex'] = "保密";
                                        break;
                                    case 1:
                                        $data['sex'] = "女";
                                        break;
                                    case 2:
                                        $data['sex'] = "男";
                                        break;
                                }
                                $strTD = <<<TD
                                    <tr>
                                        <td style='vertical-align: middle;'>{$data['id']}</td>
                                        <td style='vertical-align: middle;'>{$data['name']}</td>
                                        <td><img src='{$data['avatar']}' width='100'></td>
                                        <td style='vertical-align: middle;'>{$data['mobile']}</td>
                                        <td style='vertical-align: middle;'>{$data['sex']}</td>
                                        <td style='vertical-align: middle;'>{$data['email']}</td>
                                        <td style='vertical-align: middle;'>{$data['address']}</td>
                                        <td style='vertical-align: middle;'>{$data['dname']}</td>
                                        <td style='vertical-align: middle;'>{$data['jname']}</td>
                                        <td>
                                            <a href="personEdit.php?id={$data['id']}"><i class="icon-pencil" ></i></a>
                                            
                                            <a href="#myModal" class="del" data-id="{$data['id']}" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                        </td>
                                    </tr>
TD;
                                echo $strTD;
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
                <!-- <div class="pagination">
                    <ul>
                        <li><a href="#">上一页</a></li>
                        <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">下一页</a></li>
                    </ul>
                </div> -->

                <?php echo $showPage;?>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除确认</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>您确定要删除该员工吗？</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger" data-id="" data-dismiss="modal">删除</button>
                    </div>
                </div>

                <!-- 引用公共底部 -->
                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>
</body>

</html>
<!-- 引用script -->
<?php include_once('common/script.php'); ?>
<script>
    $('.del').click(function() {
        let id = $(this).data('id')
        console.log(id);
        $('.btn-danger').attr('data-id', id)
    });

    $('.btn-danger').click(function() {
        let id = $(this).data('id')

        //发起异步请求
        $.ajax({
            // 请求方法 get post 一般都是用post
            type: 'post',
            // 请求地址
            url: 'personList.php',
            // 请求需要的参数
            data: {
                id, //id:id
                action: 'del'
            },
            // 请求返回的数据类型
            dataType: 'json', // html text xml json
            // 请求成功的回调函数 http 状态码 = 200 
            success: function(res) {
                if (res.code === 1) {
                    alert(res.msg)
                    location.reload()
                } else {
                    alert(res.msg)
                }
            },
            // 请求失败的回调函数 404 301 500等
            error: function(error) {
                console.log(error)
            }
        })
    })
</script>